package com.lucaswangdev.util;

import org.apache.commons.collections4.CollectionUtils;

import javax.validation.ConstraintViolation;
import java.util.Set;
import java.util.stream.Collectors;

public class ValidatorUtils {
    public static <T> String validateAndReturnSingleErrorMsg(T t) {
        Set<ConstraintViolation<T>> violations = FastValidatorUtils.validate(t);
        if(CollectionUtils.isEmpty(violations)) {
            return null;
        }
        return violations.iterator().next().getMessage();
    }

    public static <T> String validateAndReturnAllErrorMsg(T t) {
        Set<ConstraintViolation<T>> violations = FastValidatorUtils.validate(t);
        if(CollectionUtils.isEmpty(violations)) {
            return null;
        }
        return violations.stream().map(ConstraintViolation::getMessage).collect(Collectors.joining(","));
    }
}
